<?php
namespace app\api\controller;

use app\common\util\WechatThirdUtil;
use app\common\model\Qrcode as QrcodeModel;
use think\Controller;
use think\Session;
use think\Db;

/**
 * 通用上传接口
 * Class Upload
 * @package app\api\controller
 */
class Upload extends Controller
{
    private $wx_user;
    private $author_wx_user;
    private $wechatObj;
    private $qrcode_model;

    protected function _initialize()
    {
        parent::_initialize();
        if (!Session::has('admin_id')) {
            $result = [
                'error'   => 1,
                'message' => '未登录'
            ];

            return json($result);
        }

        $this->qrcode_model = new QrcodeModel;
        $this->wx_user = M('wx_user')->find();
        $this->author_wx_user = Session::get('wechat_info');
        $this->wechatObj = new WechatThirdUtil($this->wx_user , $this->author_wx_user);
    }

    /**
     * 通用图片上传接口
     * @return \think\response\Json
     */
    public function upload()
    {
        $config = [
            'size' => 2097152,
            'ext'  => 'jpg,gif,png,bmp'
        ];

        if(!($file = $this->request->file('poster'))){
            $file = $this->request->file('file');
        }
        

        $upload_path = str_replace('\\', '/', ROOT_PATH . 'public/uploads');
        $save_path   = '/uploads/';
        $info        = $file->validate($config)->move($upload_path);

        if ($info) {
            $result = [
                'error' => 0,
                'url'   => str_replace('\\', '/', $save_path . $info->getSaveName())
            ];
        } else {
            $result = [
                'error'   => 1,
                'message' => $file->getError()
            ];
        }

        return json($result);
    }

    /**
     * 通用批量图片上传接口
     * @return \think\response\Json
     */
    public function batchUpload()
    {
        $config = [
            'size' => 2097152,
            'ext'  => 'jpg,gif,png,bmp'
        ];

        if(!($file = $this->request->file('poster'))){
            $file = $this->request->file('file');
        }
        
        $arr = [];
        foreach($file as $v){
            $upload_path = str_replace('\\', '/', ROOT_PATH . 'public/uploads/'.date('Ymd'));
            $save_path   = '/uploads/'.date('Ymd').'/';
            $info        = $v->validate($config)->move($upload_path , '');
    
            if ($info) {
                $result = [
                    'error' => 0,
                    'url'   => str_replace('\\', '/', $save_path . $info->getSaveName())
                ];
            } else {
                $result = [
                    'error'   => 1,
                    'message' => $v->getError()
                ];
                return false;
            }

            $arr[] = $result;
        }

        return json($arr);
    }

    /**
     * 微信客服头像上传接口
     * @return \think\response\Json
     */
    public function uploadwxheadimg()
    {
        $config = [
            'size' => 2097152,
            'ext'  => 'jpg,png'
        ];

        $file = $this->request->file('file');

        $upload_path = str_replace('\\', '/', ROOT_PATH . 'public/uploads');
        $save_path   = '/uploads/';
        $info        = $file->validate($config)->move($upload_path);

        if ($info) {
            $result = [
                'error' => 0,
                'url'   => str_replace('\\', '/', $save_path . $info->getSaveName())
            ];
        } else {
            $result = [
                'error'   => 1,
                'message' => $file->getError()
            ];
        }

        return json($result);
    }

    /**
     * 通用二维码上传接口
     * @return \think\response\Json
     */
    public function qrcode()
    {
        $config = [
            'size' => 2097152,
            'ext'  => 'jpg,gif,png,bmp'
        ];

        if(!($file = $this->request->file('qrcode'))){
            $single_img = $this->request->file('single_img');

            $upload_path = str_replace('\\', '/', ROOT_PATH . 'public/uploads');
            $save_path   = '/uploads/';
            $info        = $single_img->validate($config)->move($upload_path);
    
            if ($info) {
                $result = [
                    'error' => 0,
                    'url'   => str_replace('\\', '/', $save_path . $info->getSaveName())
                ];
            } else {
                $result = [
                    'error'   => 1,
                    'message' => $file->getError()
                ];
            }
    
            return json($result);

        }
        $arr = [];
        $media_ids = [];
        foreach($file as $v){
            $upload_path = str_replace('\\', '/', ROOT_PATH . 'public/uploads/'.date('Ymd'));
            $save_path   = '/uploads/'.date('Ymd').'/';
            $info        = $v->validate($config)->move($upload_path , '');
    
            if ($info) {
                $result = [
                    'error' => 0,
                    'url'   => str_replace('\\', '/', $save_path . $info->getSaveName())
                ];

                $img_name_path = $info->getSaveName();
                $img_name = strchr($img_name_path,'.',true);
                $path = dirname(dirname(dirname(dirname(__FILE__)))).'/public'.$result['url'];
                $info = $this->wechatObj->uploadMaterial($path, 'image');
                $media_ids[$img_name] = $info['media_id'];
            } else {
                $result = [
                    'error'   => 1,
                    'message' => $v->getError()
                ];
                return false;
            }

            $arr[] = $result;
        }

        $qrcode_media_data = [
            'auid'        => $this->author_wx_user['id'],
            'images'      => $arr,
            'media_ids'   => $media_ids,
        ];

        $res = $this->qrcode_model->save($qrcode_media_data);
        if(!$res){
            return false;
        }else{
            return json($arr);
        }
    }

    /**
     * 通用图片上传接口
     * @return \think\response\Json
     */
    public function unifiedUpload($name)
    {
        $file = $this->request->file($name);
        $config = [
            'size' => 20971520,
            'ext'  => 'jpg,gif,png,bmp'
        ];

        $upload_path = str_replace('\\', '/', ROOT_PATH . 'public/uploads');
        $save_path   = '/uploads/'; 
        $info        = $file->validate($config)->move($upload_path);

        if ($info) {
            $result = [
                'error' => 0,
                'url'   => str_replace('\\', '/', $save_path . $info->getSaveName())
            ];
        } else {
            $result = [
                'error'   => 1,
                'message' => $file->getError()
            ];
        }

        return $result;
    }
}